package com.uk.ui.raporte;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.uk.data.containers.KontrataContainer;
import com.uk.data.ejbs.IFaturaBean;
import com.uk.data.entities.Kontrata;
import com.uk.ui.kontrata.KontrataList;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.InlineDateField;
import com.vaadin.ui.NativeButton;
import com.vaadin.ui.Panel;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.themes.Reindeer;
import com.vaadin.ui.themes.Runo;

public class RaporteQueryPanel extends Panel implements Button.ClickListener{

	private IFaturaBean faturaBean;
	private InlineDateField month = new InlineDateField("Zgjidhni muajin");
	private NativeButton queryButton = new NativeButton("Shfaq raport");
	private VerticalLayout dynamicLayout = new VerticalLayout();

	
	public RaporteQueryPanel(String caption, IFaturaBean faturaBean) {
		super(caption);
		this.faturaBean = faturaBean;

		//this.setStyleName(Runo.PANEL_LIGHT);
		this.month.setResolution(InlineDateField.RESOLUTION_MONTH);
		HorizontalLayout hl = new HorizontalLayout();
		hl.addComponent(month);
		hl.addComponent(queryButton);
		hl.setComponentAlignment(queryButton, Alignment.BOTTOM_RIGHT);
		this.addComponent(hl);
		this.addComponent(dynamicLayout);
		this.queryButton.addListener(this);
		
	}
	
	private Date[] getPeriodDate(){
		Calendar c= Calendar.getInstance();
		c.setTime((Date) this.month.getValue());
		Calendar date1 = Calendar.getInstance();
		Calendar date2 = Calendar.getInstance();
		int month = c.get(Calendar.MONTH);
		int year = c.get(Calendar.YEAR);
		date1.set(year, month, 1);
		c.add(Calendar.MONTH, 1);
		date2.set(year, c.get(Calendar.MONTH), 1);
		return new Date[]{date1.getTime(), date2.getTime()};
	}

	@Override
	public void buttonClick(ClickEvent event) {

			this.listDebitor();
			
		
	}

	private void faturaAnulluar() {
		System.out.println(this.month.getValue());
		
	}

	private void listDebitor() {
		
		Date[] datat = this.getPeriodDate();
		List<Kontrata> listDebitor =  this.faturaBean.listDebitor(datat[0], datat[1]);
		System.out.println(">" + listDebitor);
		List<Kontrata> listKontrataPaFature = this.faturaBean.listKontrataPaFature(datat[0], datat[1]);
		System.out.println(">>" + listKontrataPaFature.size());
		listDebitor.addAll(listKontrataPaFature);
		System.out.println(">>>" + listDebitor);
		
		KontrataContainer kontrataContainer = new KontrataContainer(listKontrataPaFature);
		
		KontrataList kontrataList = new KontrataList(kontrataContainer);
		this.dynamicLayout.addComponent(kontrataList);
	}

	
	
}
